﻿Imports System.Windows.Forms

Public Class ListMoneyUser

#Region "重载删除事件"
    Private _Id As String = ""
    Public Overrides Function DoDelete() As Boolean
        Dim CMD As New ADODB.Command
        CMD.CommandText = "DELETE from WysInputUser  where id='" + _Id + "'"
        If ct.ExcCommand(CMD) Then
            ShowMessageBox("删除成功")
        Else
            ShowMessageBox("删除失败")
        End If
        LoadData()
        Return True
    End Function
#End Region

#Region "删除CHECK"
    Public Overrides Function DoDeleteCheck() As Boolean
        DoDeleteCheck = False
        If Me.OutlookGrid1.SelectedRows.Count = 0 Then Exit Function
        If Me.OutlookGrid1.SelectedRows(0).Cells(0).Value.ToString.Length <> 36 Then
            Return False
        End If
        _Id = Me.OutlookGrid1.SelectedRows(0).Cells(0).Value.ToString
        If ShowMessageResult("是否删除本条记录") = Windows.Forms.DialogResult.No Then Exit Function
        If _Id = "" Then
            ShowMessageBox("没有选中任何备注")
            Exit Function
        End If

        Dim rs As ADODB.Recordset = ct.GetTable("select * from WysInput  where UserName='" + _Id + "'")
        If rs IsNot Nothing AndAlso rs.EOF = False Then
            rs.Close()
            ShowMessageBox("收入表存在关联数据")
            Exit Function
        End If

        rs = ct.GetTable("select * from WysOutput  where UserName='" + _Id + "'")
        If rs IsNot Nothing AndAlso rs.EOF = False Then
            rs.Close()
            ShowMessageBox("支出表存在关联数据")
            Exit Function
        End If

        rs = ct.GetTable("select * from CrashInOut  where UserName='" + _Id + "'")
        If rs IsNot Nothing AndAlso rs.EOF = False Then
            rs.Close()
            ShowMessageBox("CrashInOut明细表存在关联数据")
            Exit Function
        End If

        rs = ct.GetTable("select * from WysBorrowLend  where LendBorrowUserName='" + _Id + "' OR UserName='" + _Id + "'")
        If rs IsNot Nothing AndAlso rs.EOF = False Then
            rs.Close()
            ShowMessageBox("WysBorrowLend明细表存在关联数据")
            Exit Function
        End If
        DoDeleteCheck = True
    End Function
#End Region

#Region "重载插入"
    Public Overrides Function DoInsert() As Boolean
        MyBase.DoInsert()
        Using frm As New MoneyUserAdd
            frm.iType = Me.iType
            frm.strName = Me.strName
            frm.ShowDialog()
            If frm.bAdd Then
                LoadData()
            End If
        End Using
        Return True
    End Function
#End Region

#Region "加载数据"
    Public Overrides Sub LoadData()
        Dim dtNew As New DataTable
        dtNew.TableName = "支出"
        dtNew.Columns.Add("编号")
        dtNew.Columns.Add("名称")
        dtNew.Columns.Add("备注")
        dtNew.Columns.Add("色彩1")
        dtNew.Columns.Add("色彩")
        Dim strSql_1 As String
        strSql_1 = "select *  from WysInputUser                 Where 1=1 "
        If rad内部.Checked Then
            strSql_1 += " AND TYPE = 0 "
        ElseIf rad借款人.Checked Then
            strSql_1 += " AND TYPE = 1 "
        ElseIf rad贷款人.Checked Then
            strSql_1 += " AND TYPE = 2 "
        End If
        strSql_1 &= " ORDER BY UserIndex "
        Dim dt As DataTable = ct.FillTable(strSql_1)
        For Each dr As DataRow In dt.Rows
            dtNew.Rows.Add(New String() {转换对象字符串(dr("id")), 转换对象字符串(dr("username")), 转换对象字符串(dr("memo")), "", 转换对象字符串(dr("COLOR"))})
        Next

        Me.OutlookGrid1.AutoGenerateColumns = False
        Me.OutlookGrid1.BindData(dtNew, "")
        Me.OutlookGrid1.Columns(0).Visible = False

        For I As Integer = 0 To Me.OutlookGrid1.RowCount - 1
            Me.OutlookGrid1.Rows(I).Cells("色彩").Style.BackColor = System.Drawing.Color.FromArgb(CInt(Me.OutlookGrid1.Rows(I).Cells("色彩1").Value))
        Next
    End Sub

    Private Function 转换对象字符串(ByVal ob As Object) As String
        If ob Is DBNull.Value Then
            Return ""
        End If
        Return ob.ToString
    End Function

    Private Function 转换对象字符串1(ByVal ob As Object) As String
        If ob Is DBNull.Value Then
            Return "内部用户"
        End If
        Select Case CInt(ob)
            Case 0
                Return "内部用户"
            Case 1
                Return "借款人"
            Case 2
                Return "贷款人"
        End Select
        Return "内部用户"
    End Function
#End Region

    Public Overrides Function DoSelect(ByVal strId As String) As Boolean
        Me._Id = strId
        Return MyBase.DoSelect(strId)
    End Function

    Private Sub rad内部_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles rad贷款人.Click, rad内部.Click, rad借款人.Click
        LoadData()
    End Sub

    Private Sub OutlookGrid1_DoubleClick(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles OutlookGrid1.DoubleClick
        If Me.OutlookGrid1.SelectedRows.Count = 0 Then Exit Sub
        If Me.OutlookGrid1.SelectedRows(0).Cells(0).Value.ToString.Length = 36 Then
            Using frm As MoneyUserAdd = New MoneyUserAdd
                frm.参数列表.Add("ID", Me.OutlookGrid1.SelectedRows(0).Cells(0).Value.ToString)
                frm.模式 = Wys.Control.Vb.InsertFormBase.模式型.修正
                frm.iType = Me.iType
                frm.strName = Me.strName
                frm.ShowDialog()

                If frm.bAdd Then
                    LoadData()
                End If
            End Using
        End If
    End Sub

    Private Sub btnUp_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles btnUp.Click
        Try
            Dim rowIndex As Integer = OutlookGrid1.SelectedRows(0).Index '得到当前选中行的索引      

            If rowIndex = 0 Then
                Exit Sub
            End If
            Dim list As New List(Of String)
            For idex As Integer = 0 To OutlookGrid1.Columns.Count - 1
                list.Add(OutlookGrid1.SelectedRows(0).Cells(idex).Value.ToString()) '把当前选中行的数据存入list数组中    
            Next

            For jdex As Integer = 0 To OutlookGrid1.Columns.Count - 1
                OutlookGrid1.Rows(rowIndex).Cells(jdex).Value = OutlookGrid1.Rows(rowIndex - 1).Cells(jdex).Value
                OutlookGrid1.Rows(rowIndex - 1).Cells(jdex).Value = list(jdex).ToString()
            Next
            OutlookGrid1.Rows(rowIndex).Selected = False
            OutlookGrid1.Rows(rowIndex - 1).Selected = True
                
        Catch ex As Exception

        End Try
    End Sub

    Private Sub btnDown_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles btnDown.Click
        Try
            Dim rowIndex As Integer = OutlookGrid1.SelectedRows(0).Index '得到当前选中行的索引      

            If rowIndex = OutlookGrid1.Rows.Count - 1 Then
                Exit Sub
            End If
            Dim list As New List(Of String)
            For idex As Integer = 0 To OutlookGrid1.Columns.Count - 1
                list.Add(OutlookGrid1.SelectedRows(0).Cells(idex).Value.ToString()) '把当前选中行的数据存入list数组中    
            Next

            For jdex As Integer = 0 To OutlookGrid1.Columns.Count - 1
                OutlookGrid1.Rows(rowIndex).Cells(jdex).Value = OutlookGrid1.Rows(rowIndex + 1).Cells(jdex).Value
                OutlookGrid1.Rows(rowIndex + 1).Cells(jdex).Value = list(jdex).ToString()
            Next
            OutlookGrid1.Rows(rowIndex).Selected = False
            OutlookGrid1.Rows(rowIndex + 1).Selected = True
        Catch ex As Exception

        End Try
    End Sub

    Private Sub Button1_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles Button1.Click

        For I As Integer = 0 To Me.OutlookGrid1.RowCount - 1
            Dim sb As New StringBuilder
            sb.AppendLine(" UPDATE WysInputUser set ")
            sb.AppendLine(" UserIndex=" & CStr(I + 1))
            sb.AppendLine("  WHERE  ")
            sb.AppendLine("  ID= '" & Me.OutlookGrid1.Rows(I).Cells("编号").Value.ToString + "'")
            ct.ExecuteSql(sb.ToString())
        Next
        LoadData()
    End Sub

End Class
